import Koa from 'koa';
import bodyParser from 'koa-bodyparser';
import { createDBClient, createfaucetView, insertDailyData, insertFiveData, insertHourData } from './view';
import views from 'koa-views';
import path from 'path'
import Schedule from 'node-schedule'
import { getTime } from './utils';
import { now } from 'mongoose';


export async function serve(config: any) {

    console.log("serve服务开启。。。。。")
    const app = new Koa();
    let { uri = 'mongodb://172.16.0.104:27017', db = 'faucet', port = 3000, tpl = './tpl' } = config.config
    const cwd = process.cwd()
    app.use(views(path.join(cwd, tpl), {
        extension: 'ejs'
    }))

    const faucetView = await createfaucetView(config)
    app.use(bodyParser())
    app.use(faucetView)
    const server = app.listen(port, () => {
        console.log(`服务启动，监听端口${port}`)
    })
}



export async function task(config: any) {
    console.log("task服务开启。。。。。")

    //每五分钟执行
    // insertHourData(config)
    // insertDailyData(config)
    // insertHourData(config)
    // insertDailyData(config)
        Schedule.scheduleJob("0 4,9,14,19,24,29,34,39,44,49,54,59 * * * ?", () => {
            insertFiveData(config)
        })


        // 每小时执行
        Schedule.scheduleJob("0 0 * * * ?", () => {
            insertHourData(config)
        })


        //每天0点执行
        Schedule.scheduleJob("0 0 0 * * ?", () => {
            insertDailyData(config)
        })
    

}
